package action.AC01;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import services.system.FileloadServices;
import com.util.Component;
import com.Const;
import com.PublicSystem;
/**
 * PersonMgrFacade:文件上传action
 * @author fcl
 * @version july 14, 2016
 * @since 1.0
 */
@Controller
@RequestMapping("files.do")
public class FileloadsAction extends HttpServlet {	
	PublicSystem sys = PublicSystem.getInstance();
	FileloadServices ls = new FileloadServices();
	private static final long serialVersionUID = 1L;
	/**
	 * 获取上传文件
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=dofile")
	public void dofile(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//创建一个通用的多部分解析器  
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求  
        if(multipartResolver.isMultipart(request)){
        //转换成多部分request
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
        //取得request中的所有文件名  
        Iterator<String> iter = multiRequest.getFileNames();
        int i = 1;
        String eventid = "null";
        String czr=Component.getSession("userid", request);
        while(iter.hasNext()){
             //记录上传过程起始时的时间，用来计算上传时间
             int pre = (int) System.currentTimeMillis();
             //取得上传文件  
             MultipartFile file = multiRequest.getFile(iter.next());
             if(file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())){
            	 //取得当前上传文件的文件名称 
                 String FileName = file.getOriginalFilename();
                 String ffType = FileName.substring(FileName.lastIndexOf(".") + 1);
          
                 if("xlsx".equals(ffType)||"xls".equals(ffType)){
                	 //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
            	     if(FileName.trim() !=""){
            	    	 
            	    	 java.util.Date de =new Date();
            			 java.text.DateFormat dated=new java.text.SimpleDateFormat("yyyyMMddHHmmss");
            			 String s_time=dated.format(de);
            			 
            		 
            	    	 //定义上传路径  
            		     String path = Const.server_path+"/page/AC01/upload/"+s_time+"."+ffType;
            		
            		     File localFile = new File(path);
            		     file.transferTo(localFile); 
            		     List list = procomponent.Excelformat.readExcel(localFile);
            		     eventid = ls.insertsql(list,"AC01",czr);
            		 }
                }else{
                	eventid="err";
                }
             }
             else
             {
            	 eventid="nofile";
             }
             //记录上传该文件后的时间  
             int finaltime = (int) System.currentTimeMillis();
           }
        Component.print(eventid, response);
        }
    }
}
